package com.example.testapplication;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.testapplication.model.User;

public class LifeCycleActivity extends AppCompatActivity {

    public static final String BUTTON_TITLE = "button_title";
    private static final String TAG = "LifeCycleActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lefecycle);
        initViews();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 999 && requestCode == RESULT_OK){
            if(data != null){
                setTitle(data.getStringExtra("imooc"));  //改变标题
            }

        }
    }

    private void initViews() {
        findViewById(R.id.buttonActivity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 跳转到SecondActivity
                 //Intent intent = new Intent(LifeCycleActivity.this,SecondActivity.class);

                // Todo  这里跳转为会bug
                //
                Intent intentI = new Intent(LifeCycleActivity.this, SecondActivity.class);
                // 传参
                // intentI.putExtra(BUTTON_TITLE,getString(R.string.codeOne)); // 这里是String

                // 第二种 Bundle  要注意传参的类型 和接收类型
                //startActivity(intent);
                Bundle bundle = new Bundle();
                bundle.putString(BUTTON_TITLE,getString(R.string.codeOne));
                intentI.putExtra(BUTTON_TITLE,bundle);
                intentI.putExtra("",new User());
                startActivity(intentI);

                startActivityForResult(intentI,999);
                // 第三种
            }
        });

        findViewById(R.id.buttonDialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(LifeCycleActivity.this)
                        .setTitle("你好")
                        .setMessage("我帅吗?")
                        .setNegativeButton("你好帅的11", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .setPositiveButton("呵呵", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .create()
                        .show();
            }
        });

        findViewById(R.id.buttonDialogActivity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 页面跳转
               // startActivity(new Intent(LifeCycleActivity.this,AlertActivity.class));
                Intent intent = new Intent(LifeCycleActivity.this,AlertActivity.class);
                startActivity(intent);
            }
        });
    }

}
